package cn.zero.service.impl;

import cn.zero.domain.AjaxResult;
import cn.zero.domain.Product;
import cn.zero.domain.Store;
import cn.zero.feign.StoreFeignClient;
import cn.zero.mapper.ProductMapper;
import cn.zero.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreFeignClient storeFeignClient;

    @Override
    @Transactional
    public AjaxResult add(Product product) {
        productMapper.add(product);
        Long productId = productMapper.loadByName(product.getName());
        Store store = new Store();
        store.setProductId(productId);
        store.setNumber(product.getNumber());
        AjaxResult add = storeFeignClient.add(store);
        if (!add.getSuccess()){
            throw new RuntimeException(add.getMessage());
        }
        return add;
    }
}
